#include "../../includes/settimer.hpp"
#include <ctime>
#include <sstream>
#include <iomanip>
using ::std::string;

timers::timers(string name,int sec){
    timers::name = name;
    diff_sec=sec;
    get_real_timer(nyear, nmonth, nday, nh, nm, nsec);
}

int timers::creat(int after_sec){
    
    return 0;
}

void timers::get_real_timer(int& year, int& month, int& day, int& hour, int& min, int& sec){
    // 获取当前时间
    std::time_t now = std::time(nullptr);
    std::tm* local_time = std::localtime(&now);
    
    // 直接赋值整数值
    year = local_time->tm_year + 1900;    // 年份 (4位数)
    month = local_time->tm_mon + 1;       // 月份 (1-12)
    day = local_time->tm_mday;            // 日期
    hour = local_time->tm_hour;           // 小时 (24小时制)
    min = local_time->tm_min;             // 分钟
    sec = local_time->tm_sec;             // 秒数
}
void timers::result_time(int& year, int& month, int& day, int& hour,int& min, int& sec){
    // 基于当前时间计算
    year = nyear;
    month = nmonth;
    day = nday;
    hour = nh;
    min = nm;
    sec = nsec + diff_sec;
    
    min += sec / 60;
    sec %= 60;
    
    hour += min / 60;
    min %= 60;
    
    day += hour / 24;
    hour %= 24;
    
    // 处理月份和年的进位
    while (day > 0) {
        int days_in_month = 31;
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            days_in_month = 30;
        } else if (month == 2) {
            days_in_month = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
        }
        
        if (day > days_in_month) {
            day -= days_in_month;
            month++;
            
            if (month > 12) {
                month = 1;
                year++;
            }
        } else {
            break;
        }
    }
}